package com.gaogzhen.introductiontoalgorithms3.divideconquer;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.RandomUtil;

/**
 * 矩阵乘法
 *  迭代算法
 * @author gaogzhen
 * @date 2024/5/31 21:28
 */
public class SquareMatrixMultiply {

    public static int[][] squareMatrixMultiply(int[][] a, int[][] b) {
        int len = a.length;
        int[][] c = new int[len][len];
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                c[i][j] = 0;
                for (int k = 0; k < len; k++) {
                    c[i][j] = c[i][j] +  a[i][k]*b[k][j];
                }
            }
        }

        return c;
    }

    public static void main(String[] args) {
        int len = 4;
        int[][] a = new int[len][len];
        int[][] b = new int[len][len];
        int[][] c;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len; j++) {
                a[i][j] = RandomUtil.randomInt(1, 10);
                b[i][j] = RandomUtil.randomInt(1, 10);
            }
        }

        c = squareMatrixMultiply(a, b);

        System.out.println(ArrayUtil.toString(a));
        System.out.println(ArrayUtil.toString(b));
        System.out.println(ArrayUtil.toString(c));
    }
}
